package com.fptt.simulator.hour;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

import com.fptt.BankAccount;
import com.fptt.Constants;
import com.fptt.DataPoint;
import com.fptt.LogFile;

public class Simulator implements Runnable
{
	private Thread thread;
	private BankAccount bank;
	private TradeManager tradeManager;
	
	private Date startDate = new Date();
	private Date endDate = new Date();
	
	public static final Double risk = 0.01;	// 1%
	public static final Double STARTING_BALANCE = 5000.00;
	
	LogFile logFile = null;

	public Simulator(String threadName) throws ParseException, IOException 
	{
			logFile = new LogFile("Data\\Trade Results.txt");
			
			startDate = Constants.mmddyyyyHDateFormat.parse("01/01/2007 0");
			endDate = Constants.mmddyyyyHDateFormat.parse("01/01/2008 0");
			
			bank = new BankAccount (STARTING_BALANCE, risk);
			
			tradeManager = new TradeManager(bank, startDate, endDate, logFile);

			thread = new Thread(this, threadName);
			thread.start();
	}
	
	@Override
	public void run() 
	{
		try
		{
			Calendar cal = Calendar.getInstance();
			cal.setTime(startDate);

			while(cal.getTime().before(endDate))
			{				
				DataPoint currentDataPoint = tradeManager.getDataSet().getDataPoint(cal.getTime());

				while (currentDataPoint == null)
				{
					cal.add(Calendar.HOUR_OF_DAY, 1);
					currentDataPoint = tradeManager.getDataSet().getDataPoint(cal.getTime());
				}
				
				tradeManager.processDataPoint(cal);
				
				Thread.sleep(0);			
				cal.add(Calendar.HOUR_OF_DAY, 1);
			}
			
			System.out.println("\nHourly Data Results:");
			System.out.println("Time Frame: " + Constants.mmddyyyyDateFormat.format(startDate) + " to " + 
				Constants.mmddyyyyDateFormat.format(endDate));
			System.out.println("Starting Balance:    " + STARTING_BALANCE);
			System.out.println("Ending Balance:    " + bank.getBalance());
			System.out.println("Net Profit: " + bank.getNetProfit());
			System.out.println("Avg $ Profit Per Data Point: " + bank.getAvgDollarProfitPerDataPoint());
			System.out.println("Avg $ Profit Per Trade: " + bank.getAvgDollarProfitPerTrade());
			System.out.println("Avg % Profit Per Data Point: " + bank.getAvgPercentProfitPerDataPoint());
			System.out.println("Avg % Profit Per Trade: " + bank.getAvgPercentProfitPerTrade());
			System.out.println("Max $ Draw Down: " + bank.getMaxDrawDown());
			System.out.println("Max $ Trade Loss: " + bank.getMaxDollarTradeLoss());
			System.out.println("Number of Trades: " + tradeManager.getNumberOfTrades() + "\n");
			
			
			logFile.write("Hourly Data Results:\n"); 
			logFile.newLine();
			logFile.write(("Time Frame: " + Constants.mmddyyyyDateFormat.format(startDate) + " to " + 
				Constants.mmddyyyyDateFormat.format(endDate) + "\n"));
			logFile.newLine();
			logFile.write("Starting Balance:    " + STARTING_BALANCE);
			logFile.newLine();
			logFile.write("Ending Balance:    " + bank.getBalance());
			logFile.newLine();
			logFile.write("Net Profit: " + bank.getNetProfit());
			logFile.newLine();
			logFile.write("Avg $ Profit Per Data Point: " + bank.getAvgDollarProfitPerDataPoint());
			logFile.newLine();
			logFile.write("Avg $ Profit Per Trade: " + bank.getAvgDollarProfitPerTrade());
			logFile.newLine();
			logFile.write("Avg % Profit Per Data Point: " + bank.getAvgPercentProfitPerDataPoint());
			logFile.newLine();
			logFile.write("Avg % Profit Per Trade: " + bank.getAvgPercentProfitPerTrade());
			logFile.newLine();
			logFile.write("Max $ Draw Down: " + bank.getMaxDrawDown());
			logFile.newLine();
			logFile.write("Max $ Trade Loss: " + bank.getMaxDollarTradeLoss());
			logFile.newLine();
			logFile.write("Number of Trades: " + tradeManager.getNumberOfTrades());
			logFile.newLine();
		}
		catch (Throwable t)
		{
 			System.out.println(t);
		}
		finally 
		{
	        //Close the BufferedWriter
	        try 
	        {
	            if (logFile != null) 
	            {
	            	logFile.flush();
	            	logFile.close();
	            }
	        } 
	        catch (IOException ex) 
	        {
	            ex.printStackTrace();
	        }
	    }
	}
	
	public static void main(String[] args) throws ParseException, IOException 
	{
		new Simulator("thread1");
	}
}

